//
//  KHPhotoBrowserController.m
//  KissHealth
//
//  Created by Mark on 2017/1/12.
//  Copyright © 2017年 Mark. All rights reserved.
//

#import "KHPhotoBrowserController.h"
#import "Masonry.h"
#import "KHPhotoBrowserCell.h"

@interface KHPhotoBrowserController ()<UICollectionViewDataSource,KHPhotoBrowserCellDelegate,UIScrollViewDelegate,UICollectionViewDelegate>

@property (nonatomic,weak)UICollectionView *collectionView;
@property (nonatomic,weak)UIButton *closeBtn;
@property (nonatomic,weak)UIButton *saveBtn;

@property (weak, nonatomic) UIPageControl *pageControl;


@end

static NSString *PhotoBrowserCell = @"PhotoBrowserCell";

@implementation KHPhotoBrowserController
- (void)loadView{
    [super loadView];
    self.view.width += 20;

}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
    
    NSIndexPath *indexPath =  [NSIndexPath indexPathForRow:self.index inSection:0];

    [self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
    

}
- (void)setupUI{
    PhotoBrowserCollectionViewLayout *layout = [[PhotoBrowserCollectionViewLayout alloc]init];
    UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:layout];
    [self.view addSubview:collectionView];
    self.collectionView = collectionView;
    
    UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];

    closeBtn.frame = CGRectMake(kSCREEN_W  - 70 , 15, 70, 32);
    closeBtn.backgroundColor = [UIColor clearColor];
    [closeBtn setTitle:@"关闭" forState:UIControlStateNormal];
    closeBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [closeBtn addTarget:self action:@selector(closeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:closeBtn];
    self.closeBtn = closeBtn;
    
    UIButton *saveBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    //saveBtn.frame = CGRectMake(kSCREEN_W - 20 -32, 20, 90, 32);

    saveBtn.backgroundColor = [UIColor darkGrayColor];
    [saveBtn setTitle:@"保存" forState:UIControlStateNormal];
    saveBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [saveBtn addTarget:self action:@selector(saveBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:saveBtn];
    self.saveBtn = saveBtn;
    
    [self.collectionView registerClass:KHPhotoBrowserCell.self forCellWithReuseIdentifier:PhotoBrowserCell];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    
    
    UIPageControl *pageControl = [[UIPageControl alloc]init];
    pageControl.pageIndicatorTintColor = [UIColor whiteColor];
    pageControl.currentPageIndicatorTintColor = [UIColor colorWithRed:0 green:183/255.0 blue:238/255.0 alpha:1];
    pageControl.numberOfPages =  self.picURLs.count;
    pageControl.currentPage = self.index ;
    pageControl.frame = CGRectMake(0, kSCREEN_H - 30, kSCREEN_W, 20);
    [self.view addSubview:pageControl];
    self.pageControl = pageControl;
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    KHPhotoBrowserCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:PhotoBrowserCell forIndexPath:indexPath];
    // 2.给cell设置数据
    cell.picURL = self.picURLs[indexPath.item];
    cell.delegate = self;
    
    return cell;

}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.picURLs.count;
}

- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSIndexPath *visiablePath = [[collectionView indexPathsForVisibleItems] firstObject];
    self.pageControl.currentPage = visiablePath.item % self.picURLs.count;
}


-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
   NSInteger index = (int)(scrollView.contentOffset.x / self.view.frame.size.width) % self.picURLs.count;
    self.pageControl.currentPage = index;
}

- (void)closeBtnClick:(UIButton *)sender{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

- (void)saveBtnClick:(UIButton *)sender{
    KHPhotoBrowserCell *cell =  (KHPhotoBrowserCell *)[self.collectionView visibleCells].firstObject;
    
    if (cell.imageView.image == nil) {
        return;
    }
    // 2.将image对象保存相册
    UIImageWriteToSavedPhotosAlbum(cell.imageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
    
}

- (void)image: (UIImage *) image didFinishSavingWithError: (NSError *) error contextInfo: (void *) contextInfo
{
    NSString *msg = nil ;
    if(error != NULL){
        msg = @"保存图片失败" ;
        [SVProgressHUD showErrorWithStatus:msg];
    }else{
        msg = @"保存图片成功" ;
        [SVProgressHUD showSuccessWithStatus:msg];

    }

}

#pragma mark cellDelegate
- (void)imageViewClick{
    [self closeBtnClick:nil];
}
@end


@implementation PhotoBrowserCollectionViewLayout

- (void)prepareLayout{
    [super prepareLayout];
    self.itemSize = self.collectionView.frame.size;
    self.minimumInteritemSpacing = 0;
    self.minimumLineSpacing = 0;
    self.scrollDirection =UICollectionViewScrollDirectionHorizontal;
    
    self.collectionView.pagingEnabled = YES;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.showsVerticalScrollIndicator = NO;
}
@end

